/**
 * 
 */
package com.lemote.entity;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.lemote.utils.reflection.ConvertUtils;

/**
 * @author wuhui
 *
 * 下午02:03:01
 */
@Entity
@Table(name="smt_role")
//默认的缓存策略.
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends IdEntity {

	private String roleCname;
	
	private String roleEname;
	
	private String status;
	
	private List<Permission> permissions;
	
	private List<Model> models;


	@ManyToMany(fetch = FetchType.LAZY)   
	@JoinTable(name="smt_role_model", joinColumns={@JoinColumn(name="roleid")}, inverseJoinColumns={@JoinColumn(name="modelid")})
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Model> getModels() {
		return models;
	}
	@ManyToMany
	@JoinTable(name = "smt_role_permission", joinColumns = { @JoinColumn(name = "roleid") }, inverseJoinColumns = { @JoinColumn(name = "permissionid") })
	@OrderBy("id")
	@Fetch(FetchMode.SUBSELECT)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Permission> getPermissions() {
		return permissions;
	}

	public String getRoleCname() {
		return roleCname;
	}

	public String getRoleEname() {
		return roleEname;
	}

	public String getStatus() {
		return status;
	}
	

	public void setModels(List<Model> models) {
		this.models = models;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	public void setRoleCname(String roleCname) {
		this.roleCname = roleCname;
	}

	public void setRoleEname(String roleEname) {
		this.roleEname = roleEname;
	}

	public void setStatus(String status) {
		this.status = status;
	}
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getPermissionIds() {
		return ConvertUtils.convertElementPropertyToList(permissions, "id");
	}
	@Transient
	@SuppressWarnings("unchecked")
	public List<Long> getModelIds() {
		return ConvertUtils.convertElementPropertyToList(models, "id");
	}
	
}
